﻿using AutoMapper;
using MediatR;
using SmartProduction.Domain.采购委外订单;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue.Interface;
using SmartProduction.Write.API.Application.Commands.PurchaseCommand;

namespace SmartProduction.Write.API.Application.CommandHandlers.PurchaseCommandHandler
{
    public class PurchaseStatusUpdateCommandHandler : IRequestHandler<PurchaseStatusUpdateCommand, APIResult<object>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<Purchase> purchaseRepository;

        public PurchaseStatusUpdateCommandHandler(IMapper mapper, IBaseRepository<Purchase> purchaseRepository)
        {
            this.mapper = mapper;
            this.purchaseRepository = purchaseRepository;
        }

        public async Task<APIResult<object>> Handle(PurchaseStatusUpdateCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            var entiyi = purchaseRepository.GetAll().Result.FirstOrDefault(x => x.Purchaseid == request.Purchaseid);
            entiyi.ApprovalStatus = request.ApprovalStatus;
            var i = purchaseRepository.UpdateAsync(entiyi).Result;
            if (i > 0)
            {
                result.Code = APIEnums.修改成功;
                result.Msg = APIEnums.修改成功.ToString();
            }
            else
            {
                result.Code = APIEnums.修改失败;
                result.Msg = APIEnums.修改失败.ToString();
            }
            result.data = i;
            return await Task.FromResult(result);
        }
    }
}
